const {spawn} = require('child_process');
const fs = require('fs');
const path = require('path');

let viteProcess;

function startVite() {
    viteProcess = spawn('npm', ['run', 'dev'], {stdio: 'inherit'});
}

startVite();

// 监视 public 目录下的 config.json 文件的变化
const configFile = path.resolve(__dirname, 'public', 'config.json');
fs.watch(configFile, (eventType, filename) => {

    console.log(eventType)
    if (eventType === 'change') {
        console.log('public/config.json has changed. Restarting Vite server...');

        // 结束 Vite 进程
        viteProcess.kill();

        // 重新启动 Vite 开发服务器
        startVite();
    }
});
